package com.ruoyi.web.controller.tag;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.oos.tag.domain.TagGroup;
import com.ruoyi.oos.tag.service.ITagGroupService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 测点分组Controller
 * 
 * @author wzy
 * @date 2020-05-07
 */
@Controller
@RequestMapping("/tag/group")
public class TagGroupController extends BaseController
{
    private String prefix = "tag/group";

    @Autowired
    private ITagGroupService tagGroupService;

    @GetMapping()
    public String group()
    {
        return prefix + "/group";
    }

        /**
     * 查询测点分组列表
     */
    @PostMapping("/list")
    @ResponseBody
    public  List<TagGroup> list(TagGroup tagGroup)
    {
        List<TagGroup> list = tagGroupService.selectTagGroupList(tagGroup);
        return list;
    }
    
    /**
     * 导出测点分组列表
     */
    @Log(title = "测点分组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TagGroup tagGroup)
    {
        List<TagGroup> list = tagGroupService.selectTagGroupList(tagGroup);
        ExcelUtil<TagGroup> util = new ExcelUtil<TagGroup>(TagGroup.class);
        return util.exportExcel(list, "group");
    }

    /**
     * 新增测点分组
     */
    @GetMapping("/add/{id}")
    public String add(@PathVariable("id") Long id, ModelMap mmap)
    {
        TagGroup tagGroup = tagGroupService.selectTagGroupById(id);
        tagGroup.setPname(tagGroup.getGroupname());
        
        mmap.put("tagGroup", tagGroup);
        return prefix + "/add";
    }

    /**
     * 新增保存测点分组
     */
    @Log(title = "测点分组", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TagGroup tagGroup)
    {
        return toAjax(tagGroupService.insertTagGroup(tagGroup));
    }

    /**
     * 修改测点分组
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TagGroup tagGroup = tagGroupService.selectTagGroupById(id);
        TagGroup tagGroup1 =tagGroupService.selectTagGroupById(tagGroup.getPid());
        tagGroup.setPname(tagGroup1.getGroupname());
        
        mmap.put("tagGroup", tagGroup);
        return prefix + "/edit";
    }

    /**
     * 修改保存测点分组
     */
    @Log(title = "测点分组", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TagGroup tagGroup)
    {
        return toAjax(tagGroupService.updateTagGroup(tagGroup));
    }

    /**
     * 删除测点分组
     */
    @Log(title = "测点分组", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tagGroupService.deleteTagGroupByIds(ids));
    }
    
    /**
     * 加载部门列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = tagGroupService.selectDeptTree(new TagGroup());
        return ztrees;
    }
    
    /**
     * 选择部门树
     */
    @GetMapping("/selectDeptTree/{id}")
    public String selectGroupTree(@PathVariable("id") Long id, ModelMap mmap)
    {
        mmap.put("group", tagGroupService.selectTagGroupById(id));
        return prefix + "/tree";
    }
  
}
